#! /usr/bin/env python

import sys, os, unittest, logging

testsdir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, testsdir)

os.environ['TEST_DIRECTORY'] = 'tmp'
if not os.path.exists('tmp'):
    os.mkdir('tmp')

from soup.env import EnvironmentLoader

known_failures = [
    'Evo',          # Will trash your main evo :/
    'Audio',        # GStreamer hanging
    'Video',        # Probably same as ^
]

def run_tests(tests, opts):
    runner = test.TestRunner(opts)

    if opts.verbose:
        logging.basicConfig(level=logging.DEBUG)

    res = runner.run(unittest.TestSuite(tests))

def list_tests(tests, opts):
    for test in tests:
        print test.name(), test.testMethodName

import optparse

parser = optparse.OptionParser(usage="usage: %prog [options] arg1 arg2")

# Options about which tests are run
parser.add_option("-x", "--exclude", action="append", type="string", dest="exclude",
                  help="Exclude patterns matching PATTERN", metavar="PATTERN")

# Options about how the tests are run
parser.add_option("-r", "--randomize", action="store_true", dest="randomize",
                  help="Run tests in a random order")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                  help="Output lots of noise as tests are run")
parser.add_option("-c", "--coverage", action="store_true", dest="coverage",
                  help="Enable code coverage")
parser.add_option("-p", "--profile", action="store_true", dest="profile",
                  help="Profile execution")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
                  help="Run test case in debugger")
parser.add_option("-z", "--lazy-debug", action="store_true", dest="lazydebug",
                  help="Break into pdb on unexpected exceptions")

# Add the different execution modes..
parser.add_option("-l", "--list", action="store_const", const="list", dest="mode",
                  help="List tests instead of running them")
parser.add_option("-e", "--execute", action="store_const", const="execute", dest="mode",
                  help="Run the tests")

# Set parse defaults
parser.set_defaults(mode="execute", randomize=False, verbose=False, coverage=False,
                    exclude=known_failures, debug=False, lazydebug=False)

# And parse..
opts, args = parser.parse_args()

EnvironmentLoader.prepare_environment(opts)

import soup
from soup.utils import test

from test_datatypes import *
from test_dataprovider import *
from test_synchronization import *

# Figure out which tests to run
tests = test.TestLoader(include=args, exclude=opts.exclude).loadTestsFromMain()

if opts.randomize:
   import random
   random.shuffle(tests)

# And run.
if opts.mode == "execute":
    run_tests(tests, opts)
elif opts.mode == "list":
    list_tests(tests, opts)

EnvironmentLoader.finalize_environment()

sys.exit(0)
